package com.zys.el.time.controller;

import com.zys.el.time.common.Result;
import com.zys.el.time.entity.Channel;
import com.zys.el.time.entity.ChannelSubscribe;
import com.zys.el.time.service.ChannelService;
import com.zys.el.time.service.ChannelSubscribeService;
import com.zys.el.time.vojo.ChannelVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "频道接口")
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/channel")
public class ChannelController {
    @Autowired
    private ChannelService channelService;
    @Autowired
    private ChannelSubscribeService channelSubscribeService;

    @GetMapping("/list")
    @ApiOperation("获取全部频道")
    public Result<List<ChannelVo>> getList() {
        List<ChannelVo> voList = channelService.list().stream().map(c -> {
            ChannelVo channelVo = new ChannelVo();
            BeanUtils.copyProperties(c, channelVo);
            Long count = channelSubscribeService.query().eq("channel_id", c.getId()).count();
            channelVo.setMember(Math.toIntExact(count));
            return channelVo;
        }).collect(Collectors.toList());
        return Result.ok(voList);
    }

    @GetMapping("/list/{userId}")
    @ApiOperation("获取此用户加入的频道")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true)
    public Result getListByUserId(@PathVariable String userId) {
        log.debug(userId);
        if (StringUtils.isBlank(userId)) return Result.fail("参数有误");
        List<ChannelVo> channels = channelService.getByUserId(Long.valueOf(userId));
        return Result.ok(channels);
    }

    @GetMapping("")
    @ApiOperation("获取频道详情By频道id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "channelId", value = "频道id", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true)
    })
    public Result getByChannelId(@RequestParam("channelId") String channelId,
                                 @RequestParam("userId") String userId) {
        if (StringUtils.isBlank(channelId) || StringUtils.isBlank(userId)) return Result.fail("参数有误");
        ChannelVo channelVo = channelService.getChannelInfoById(Long.valueOf(channelId), Long.valueOf(userId));
        return Result.ok(channelVo);
    }


    @PostMapping("/add")
    @ApiOperation("添加频道")
    public Result add(@RequestBody Channel channel) {
//        log.debug(channel.toString());
        channelService.saveAndSubscribe(channel);
        return Result.ok("添加频道成功");
    }

    @DeleteMapping("/{channelId}")
    @ApiOperation("删除频道")
    @ApiImplicitParam(name = "channelId", value = "频道id", required = true)
    public Result del(@PathVariable String channelId) {
//        log.debug(channelId);
        channelService.removeByIdWithSubscribe(Long.valueOf(channelId));
        return Result.ok("删除频道成功");
    }

    @PostMapping("/modify")
    @ApiOperation("修改频道")
    public Result modify(@RequestBody Channel channel) {
//        log.debug(channel.toString());
        boolean b = channelService.updateById(channel);
        if (b) {
            return Result.ok("修改成功");
        }
        return Result.fail("修改失败");
    }


    @PostMapping("subscribe")
    @ApiOperation("修改用户订阅频道状态")
    public Result subscribe(@RequestBody ChannelSubscribe channelSubscribe) {
        log.debug(String.valueOf(channelSubscribe));

        channelSubscribeService.changeSubStatus(channelSubscribe);

        return Result.ok(channelSubscribe.getStatus() ? "加入成功" : "退出成功");
    }

}
